package com.kwan.widget.tool.recycle;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.youth.banner.util.LogUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author kwan
 * @date 2023/3/23 10:08
 * @Describe
 */
public class XRecycleBuilder<T> implements OnLoadMoreListener, OnRefreshListener {

	RecyclerView mRecyclerView;
	RecyclerView.LayoutManager mLayoutManager;

	public BaseQuickAdapter<T, BaseViewHolder> mAdapter;

	Context mContext;

	public XRecycleBuilder(Context context, @NonNull RecyclerView recyclerView, BaseQuickAdapter<T, BaseViewHolder> adapter) {
		mContext = context;
		mRecyclerView = recyclerView;
		mAdapter = adapter;
	}

	public void build() {

		if (mLayoutManager == null)
			mLayoutManager = new LinearLayoutManager(mContext);


		mAdapter.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> a, @NonNull @NotNull View view, int position) {
				if (callBack != null) {
					callBack.onItemClick(mAdapter.getItem(position), position);
				}
			}
		});

		mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
			@Override
			public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter<?, ?> a, @NonNull @NotNull View view, int position) {
				if (callBack != null) {
					callBack.onItemChildClick(view, (T) mAdapter.getItem(position), position);
				}
			}
		});


		mRecyclerView.setLayoutManager(mLayoutManager);

		if (list_empty_view != 0) {
			mAdapter.setEmptyView(LayoutInflater.from(mContext).inflate(list_empty_view, null));
		}


		mRecyclerView.setAdapter(mAdapter);


	}


	public void setListData(List<T> data) {
		mAdapter.setNewInstance(data);
	}

	public void notifyDataSetChanged() {
		mAdapter.notifyDataSetChanged();
	}


	public XRecycleBuilder<T> layoutManager(RecyclerView.LayoutManager layoutManager) {
		mLayoutManager = layoutManager;
		return this;
	}


	public SmartRefreshLayout mRefreshLayout;


	private static final int PAGE_SIZE = 10;

	private int pageNum = 1;
	private int pageSize = PAGE_SIZE;
	private boolean hasMore = true;

	public XRecycleBuilder<T> refreshLayout(SmartRefreshLayout refreshLayout, boolean isRefresh, boolean isLoadMore) {
		refreshLayout.setEnableRefresh(isRefresh);
		refreshLayout.setEnableLoadMore(isLoadMore);
		refreshLayout.setOnRefreshListener(this);
		refreshLayout.setOnLoadMoreListener(this);
		mRefreshLayout = refreshLayout;
		return this;
	}


	View loadEndView;
	@LayoutRes
	int loadEndViewId;

	public XRecycleBuilder<T> loadEndView(View view) {
		loadEndView = view;
		return this;
	}

	public XRecycleBuilder<T> loadEndView(@LayoutRes int id) {
		loadEndView = LayoutInflater.from(mContext).inflate(id, mRecyclerView, false);
		return this;
	}


	public XRecycleBuilder<T> pageSize(int pageSize) {
		this.pageSize = pageSize;
		return this;
	}


	@Override
	public void onRefresh(@NonNull RefreshLayout refreshLayout) {
		refreshLayout.setEnableLoadMore(true);
		mAdapter.removeAllFooterView();
		hasMore = true;
		pageNum = 1;
		LogUtils.i("onRefresh ... ");
		if (callBack != null) {
			callBack.onRefresh(refreshLayout);
			callBack.doRunTask(1, pageSize);
		} else {
			refreshLayout.finishRefresh();
		}
	}

	@Override
	public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
		pageNum++;
		if (callBack != null) {
			callBack.onLoadMore(refreshLayout);
			callBack.doRunTask(pageNum, pageSize);
		} else {
			refreshLayout.finishLoadMore();
		}
	}

	public void doOnGetNewData(List<T> data) {

		if (mRefreshLayout != null) {
			mRefreshLayout.finishRefresh();
			mRefreshLayout.finishLoadMore();
		}

		if (data == null || data.isEmpty()) {
			if (pageNum == 1) {
				mAdapter.setList(new ArrayList<>());
				mRecyclerView.setAdapter(mAdapter);
			}
			return;
		}

		if (data.size() < pageSize) {
			hasMore = false;
			if (mRefreshLayout != null)
				mRefreshLayout.setEnableLoadMore(false);

			if (pageNum == 1 && data.size() == 0) {
				mAdapter.setList(new ArrayList<>());
				mRecyclerView.setAdapter(mAdapter);
			} else {
				mAdapter.removeAllFooterView();
				if (loadEndView != null)
					mAdapter.addFooterView(loadEndView);
			}
		}

		if (pageNum == 1) {
			mAdapter.setList(data);
			mRecyclerView.setAdapter(mAdapter);
		} else {
			mAdapter.addData(data);
		}
	}


	private CallBack<T> callBack;

	public XRecycleBuilder<T> callBack(CallBack<T> callBack) {
		this.callBack = callBack;
		return this;
	}


	int list_empty_view;

	public void setEmptyView(int list_empty_view) {

		this.list_empty_view = list_empty_view;
	}

	public interface CallBack<T> {
		void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout);

		void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout);

		void doRunTask(int pageNum, int pageSize);

		void onItemClick(T item, int position);

		void onItemChildClick(View v, T item, int position);
	}

	public abstract static class SimpleCallBack<T> implements CallBack<T> {

		@Override
		public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {

		}

		@Override
		public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {

		}

	}
}
